package com.user.controller;

import ch.qos.logback.core.subst.Token;
import com.entity.MyToken;
import com.user.service.CodeServiceFeigClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
   @Autowired
   private CodeServiceFeigClient codeServiceFeigClient;

    /**
     * 用户注册
     * @return
     */
    @GetMapping("/register/{email}/{pwd}/{code}")
    public Map register(@PathVariable String email,@PathVariable String pwd,@PathVariable String code){
        Object details = SecurityContextHolder.getContext().getAuthentication().getDetails();


        Map<String,Object> result = new HashMap<>();
//         对验证码进行验证
        Map valdate = codeServiceFeigClient.valdate(email, code);
        if(valdate.get("success").equals("0")){
            MyToken.USER_MAP.put(email,pwd);
//            生成token
            MyToken.TOKEN_MAP.put(email, UUID.randomUUID());


        }

        return  result;
    }

    /**
     * 是否注册
     * @param email
     * @return
     */
    @GetMapping("/isRegister/{email}")
    public Map isRegister(@PathVariable String email){
        Map<String,Object> result = new HashMap<>();
          if(MyToken.USER_MAP.containsKey(email)){
              result.put("error",false);
          }else {
              result.put("success",true);
          }
        return  result;
    }

    /**
     * 用户登录
     * @param email
     * @param pwd
     * @return
     */
    @GetMapping("/login/{email}/{pwd}")
    public Map login(@PathVariable String email,@PathVariable String pwd){
        Map<String,Object> result = new HashMap<>();
        String getPwd = MyToken.USER_MAP.get(email).toString();
        if(getPwd.equals(pwd)){
            //生成token
            MyToken.TOKEN_MAP.put(email,UUID.randomUUID());
        }else {
            result.put("error",false);
        }
        return  result;
    }
}
